package cn.tedu.ivos.geofence.service.serviceImp;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GeofenceServiceImp implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> query(GeofenceQuery geofenceQuery) {
        List<GeofenceVO> list =  geofenceMapper.query(geofenceQuery);
        return list;
    }

    @Override
    public void save(GeofenceSaveParam geofenceSaveParam) {
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam,geofence);
        if (geofence.getId()== null)
        {
            System.out.println("新增");
            geofence.setCreateTime(new Date());
            geofence.setStatus("0");
            geofenceMapper.save(geofence);
        }
        else
        {
            System.out.println("更新");
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }

    }

    @Override
    public void del(Long id) {
/*
    正经处理的方式
    先提示有车辆没移除
 */
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(String.valueOf(id));

        List<VehicleVO> list = vehicleMapper.query(vehicleQuery);
        if (list.size() >0 && list !=null)
        {
            throw new ServiceException(StatusCode.Vehicle_EXITS);
        }else
        {
            Geofence geofence = new Geofence();
            geofence.setId(id);
            geofenceMapper.del(geofence);
        }
        /*
        Vehicle vehicle = new Vehicle();
        vehicle.setGeofenceId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update1(vehicle);
         */

    }


}
